<?php
namespace App\Admin\Exports;

use App\Models\Shop\WxShopOrderGoods;
use App\Models\WxAuthentication;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\UserUtils;
use Dcat\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;

class CommonAdminCollectExport extends AbstractExporter
{
    private $file_name = 'test';
    private $model;

    public function __construct($file_name, $model)
    {
        if(!_empty_($file_name)){
            $this->file_name = $file_name;
        }
        $this->model = $model;
    }

    public function export()
    {
        global $__MINI_GLOBAL_PAYCODE_0__;
        if(!$__MINI_GLOBAL_PAYCODE_0__){
            exit('未授权用户不能使用该功能');
        }
        $exportobj = null;
        $models = [];
        $headings = [];
        if($this->model == 'WxShopOrder'){
            $headings = [
                'id', '用户id', '用户名', '认证名称' ,'购物清单', '商品总价', '优惠金额', '订单总额', '已支付的金额', '支付流水号', '使用金币', '金币抵扣￥', '支付状态',
                '订单状态', '用户备注', '商家备注', '支付时间', '发货时间', '配送费用', '收货人姓名', '收货人电话', '收货详细地址', '物流名称', '物流单号',
                '是否存在虚拟商品', '确认收货时间', '订单创建时间'
            ];
            $pay_status = [1 => '未付款', 2 => '已付款', 3 => '已退款'];
            $status_ = [0 => '正常', 1 => '未发货', 2 => '已发货', 3 => '确认收货', 4 => '已退货', 5 => '取消', 6 => '用户删除', 7=>'退款中', 8=>'自提发货待确定'];

            collect($this->buildData(1, 100000))->map(function ($v) use (&$models, $pay_status, $status_){
                $goods = WxShopOrderGoods::where('order_id', $v['id'])->pluck('name');
                $goods_str = "";
                if($goods){
                    $index = 1;
                    foreach ($goods as $good){
                        $goods_str .= '('.$index.')'.$good ."\r\n        ";
                        $index ++;
                    }
//                    $goods_str = nl2br($goods_str);
                }
                $models[] = [
                    'id' => $v['id'],
                    'user_id' => $v['user_id'],
                    'user_name' => UserUtils::get_cached_user($v['user_id'])['user_name'],
                    'authentication' => WxAuthentication::where('user_id', $v['user_id'])->where('authentication_state', 1)->orderBy('id', 'desc')->value('name'),
                    'goods_list' => $goods_str,
                    'goods_amount' => $v['goods_amount'],
                    'discounts_amount' => $v['discounts_amount'],
                    'order_amount' => $v['order_amount'],
                    'payed' => $v['payed'],
                    'serial_number' => $v['serial_number'],
                    'coins_num' => $v['coins_num'],
                    'coins_pay' => $v['coins_pay'],
                    'pay_status' =>  $pay_status[(int)($v['pay_status'])],
                    'status' => $status_[(int)($v['status'])],
                    'user_remark' => $v['user_remark'],
                    'remark' => $v['remark'],
                    'payment_time' => $v['payment_time'],
                    'ship_at' => $v['ship_at'],
                    'cost_freight' => $v['cost_freight'],
                    'adds_name' => $v['adds_name'],
                    'adds_mobile' => $v['adds_mobile'],
                    'address' => $v['address'],
                    'express' => $v['express'],
                    'express_no' => $v['express_no'],
                    'has_virtual' => $v['has_virtual'] == 1 ? '是' : '否',
                    'confirm_time' => $v['confirm_time'],
                    'created_at' => $v['created_at'],
                ];
            });

        }else if($this->model == 'WxUserLuckDrawRecord'){
            $headings = ['id',  '用户id', '用户名', '认证名称', '付费方式', '奖品类型', '奖品数量', '说明', '抽奖时间'];

            $pay_way = FieldUtils::getPayTypes();
            $ward_types  = FieldUtils::getGiftTypes('luckdraw');

            collect($this->buildData(1, 100000))->map(function ($v) use (&$models, $pay_way, $ward_types){
                $models[] = [
                    'id' => $v['id'],
                    'user_id' => $v['user_id'],
                    'user_name' => UserUtils::get_cached_user($v['user_id'])['user_name'],
                    'authentication' => WxAuthentication::where('user_id', $v['user_id'])->where('authentication_state', 1)->orderBy('id', 'desc')->value('name'),
                    'pay' => $pay_way[(int)($v['pay'])],
                    'type' => $ward_types[(int)($v['type'])],
                    'num' => $v['num'],
                    'tip' => $v['tip'],
                    'created_at' => $v['created_at'],
                ];
            });
        }

        $models = collect($models);
        if($models){
            $models = $models->sortByDesc(function ($v, $key){
                return (int)($v['id']);
            });
        }

        $exportobj = new \App\Exports\CollectionExport($models, $headings);

        if($exportobj){
            $this->file_name = $this->file_name . '_' . time() . '.xlsx';
            $res = Excel::store($exportobj, $this->file_name, 'local');
            if($res){
                $cache_html = '<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="refresh" content="0;url=/common/download?path='.$this->file_name.'"> <title>Redirecting</title> </head> <body><h1>导出成功</h1></body> </html>';
                exit($cache_html);
            }else{
                exit('出现错误');
            }
        }else{
            exit('联系群主微信：ShaoWenSir');
        }

    }
}
